package com.example.papercut.controller;

import com.example.papercut.entity.AuthorInformation;
import com.example.papercut.entity.PapercutTemplate;
import com.example.papercut.service.AuthorInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/authors")
public class AuthorController {

    private final AuthorInformationService authorService;

    @Autowired
    public AuthorController(AuthorInformationService authorService) {
        this.authorService = authorService;
    }

    /**
     * GET /api/authors/{id}
     * 4.1: 获取作者详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<AuthorInformation> getAuthorDetails(@PathVariable Integer id) {
        AuthorInformation author = authorService.getAuthorDetails(id);
        if (author == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(author);
    }

    /**
     * GET /api/authors/style/{styleId}
     * 4.2: 根据派系 ID 获取所有作者
     */
    @GetMapping("/style/{styleId}")
    public ResponseEntity<List<AuthorInformation>> getAuthorsByStyle(@PathVariable Integer styleId) {
        List<AuthorInformation> authors = authorService.getAuthorsByStyleId(styleId);
        return ResponseEntity.ok(authors);
    }
    
    /**
     * GET /api/authors/style/{styleId}/templates
     * 4.2: 获取指定派系的所有模板
     */
    @GetMapping("/style/{styleId}/templates")
    public ResponseEntity<List<PapercutTemplate>> getTemplatesByStyle(@PathVariable Integer styleId) {
        // 注意：Mapper 层已实现，但在 Service 层需将 AuthorInformationMapper 的 List<AuthorInformation> 
        // 查询结果转换或调用 PapercutTemplateMapper 实现，这里假设 Service 直接返回 List<PapercutTemplate>
        List<PapercutTemplate> templates = authorService.getTemplatesByStyleId(styleId); 
        return ResponseEntity.ok(templates);
    }
}